--DO NOT MODIFY THIS FILE. IT IS ALWAYS OVERWRITTEN ON GENERATION.
--Data Schema For Version 0.0.0.0
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_TerritoryDelete]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_TerritoryDelete]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_TerritoryDelete]
(
	@Original_TerritoryID NVarChar(20)
)
AS
SET NOCOUNT OFF;

DELETE FROM 
	[Territories] 
WHERE 
	[TerritoryID] = @Original_TerritoryID ;

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_TerritorySelectByTerritoryPks]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_TerritorySelectByTerritoryPks]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO


CREATE PROCEDURE [dbo].[gen_TerritorySelectByTerritoryPks]
(
	@xml ntext
)
AS

DECLARE @hDoc int
EXEC sp_xml_preparedocument @hDoc OUTPUT, @xml

SELECT
		[Territories].[territoryid],
	[Territories].[territorydescription],
	[Territories].[regionid],
	[Territories].[created_by],
	[Territories].[created_date],
	[Territories].[modified_by],
	[Territories].[modified_date],
	[Territories].[time_stamp]

FROM 
[Territories]
WHERE

	[Territories].[TerritoryID] IN (SELECT [TerritoryID] 
											FROM OpenXML(@hDoc, '//Territories', 2) 
											WITH ([TerritoryID] char(36) 'TerritoryID')) 


exec sp_xml_removeDocument @hDoc

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_TerritorySelectByTerritorySinglePk]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_TerritorySelectByTerritorySinglePk]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_TerritorySelectByTerritorySinglePk]
(
	@TerritoryID NVarChar(20)
)
AS

SELECT 
	[Territories].[territoryid],
	[Territories].[territorydescription],
	[Territories].[regionid],
	[Territories].[created_by],
	[Territories].[created_date],
	[Territories].[modified_by],
	[Territories].[modified_date],
	[Territories].[time_stamp]

FROM
[Territories]
WHERE 
[Territories].[TerritoryID] = @TerritoryID 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_TerritoryInsert]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_TerritoryInsert]
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS ON
GO

CREATE PROCEDURE [dbo].[gen_TerritoryInsert]
(
	@TerritoryID NVarChar(20) = default,
	@TerritoryDescription NChar(50) = default,
	@RegionID Int = default,
	@created_date datetime,
	@created_by varchar (50),
	@modified_by varchar (50)

)
AS
SET NOCOUNT OFF;

if (@created_date IS NULL)
SET @created_date = GetDate()

INSERT INTO [Territories]
(
[RegionID],
[TerritoryDescription],
[TerritoryID],
[created_date],
[created_by],
[modified_date],
[modified_by]

)
VALUES
(
	@RegionID,
	@TerritoryDescription,
	@TerritoryID,
	@created_date,
	@created_by,
	@created_date,
	@modified_by

);


SELECT 
	[Territories].[territoryid],
	[Territories].[territorydescription],
	[Territories].[regionid],
	[Territories].[created_by],
	[Territories].[created_date],
	[Territories].[modified_by],
	[Territories].[modified_date],
	[Territories].[time_stamp]

FROM
[Territories]
WHERE
	[Territories].[TerritoryID] = @TerritoryID;
GO

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_TerritoryPagingSelect]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_TerritoryPagingSelect]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_TerritoryPagingSelect]
(
	@page int, -- page number selected by the user
	@pageSize int, -- number of items on the page
	@orderByColumn varchar(100), -- name of column to order things by
	@ascending bit, -- order column ascending or descending
	@filter varchar(100) = null, -- filter statement passed in to determine like criteria on order by column
	@count int out -- number of items in the collection
)
AS

SET NOCOUNT ON;


CREATE TABLE #tmpTable
(
[TerritoryID] NVarChar(20)
)

DECLARE @total__ivqatedr int
DECLARE @orderByColumnIndex int
-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@pageSize * @page)
IF (@total__ivqatedr <> 0)
BEGIN
	SET ROWCOUNT @total__ivqatedr
END
INSERT INTO #tmpTable
(
	[territoryid]
)
SELECT
	[Territories].[territoryid]
FROM
[Territories]
WHERE
	(@orderByColumn = 'TerritoryID' and (((@filter is null) or ([Territories].[TerritoryID] is null)) or (@filter is not null and [Territories].[TerritoryID] LIKE @filter)))
or
	(@orderByColumn = 'TerritoryDescription' and (((@filter is null) or ([Territories].[TerritoryDescription] is null)) or (@filter is not null and [Territories].[TerritoryDescription] LIKE @filter)))
or
	(@orderByColumn = 'RegionID' and (((@filter is null) or ([Territories].[RegionID] is null)) or (@filter is not null and [Territories].[RegionID] = @filter)))
ORDER BY
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'TerritoryID' THEN [Territories].[TerritoryID] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'TerritoryID' THEN [Territories].[TerritoryID] END END, 
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'TerritoryDescription' THEN [Territories].[TerritoryDescription] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'TerritoryDescription' THEN [Territories].[TerritoryDescription] END END, 
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'RegionID' THEN [Territories].[RegionID] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'RegionID' THEN [Territories].[RegionID] END END

-- set @count based on the rows moved in the previous statement
SET ROWCOUNT 0
SET @count = (
SELECT count(*)
FROM
[Territories]
WHERE
	(@orderByColumn = 'TerritoryID' and (((@filter is null) or ([Territories].[TerritoryID] is null)) or (@filter is not null and [Territories].[TerritoryID] LIKE @filter)))
or
	(@orderByColumn = 'TerritoryDescription' and (((@filter is null) or ([Territories].[TerritoryDescription] is null)) or (@filter is not null and [Territories].[TerritoryDescription] LIKE @filter)))
or
	(@orderByColumn = 'RegionID' and (((@filter is null) or ([Territories].[RegionID] is null)) or (@filter is not null and [Territories].[RegionID] = @filter)))
)

-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@pageSize * @page) - @pageSize
IF (@total__ivqatedr <> 0)
BEGIN
	SET ROWCOUNT @total__ivqatedr
	DELETE FROM #tmpTable
END

-- return the number of rows requested as the page size
SET ROWCOUNT @pageSize
SELECT
	[Territories].[territoryid],
	[Territories].[territorydescription],
	[Territories].[regionid],
	[Territories].[created_by],
	[Territories].[created_date],
	[Territories].[modified_by],
	[Territories].[modified_date],
	[Territories].[time_stamp]
FROM
	[#tmpTable]
	INNER JOIN [Territories] ON #tmpTable.[territoryid] = [TERRITORIES].[territoryid]
ORDER BY
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'TerritoryID' THEN [Territories].[TerritoryID] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'TerritoryID' THEN [Territories].[TerritoryID] END END, 
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'TerritoryDescription' THEN [Territories].[TerritoryDescription] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'TerritoryDescription' THEN [Territories].[TerritoryDescription] END END, 
	CASE @ascending WHEN 0 THEN CASE @orderByColumn WHEN 'RegionID' THEN [Territories].[RegionID] END END DESC, 
	CASE @ascending WHEN 1 THEN CASE @orderByColumn WHEN 'RegionID' THEN [Territories].[RegionID] END END

DROP TABLE #tmpTable

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_TerritorySelectBySearchAnd]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_TerritorySelectBySearchAnd]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_TerritorySelectBySearchAnd]
(
	@TerritoryID NVarChar(20) = null, 
	@TerritoryDescription NChar(50) = null, 
	@RegionID Int = null, 
	@paging_PageIndex int = -1, -- page number selected by the user
	@paging_RecordsperPage int = -1, -- number of items on the page
	@paging_OrderByColumn varchar(100) = '', -- name of column to order things by
	@paging_Ascending bit = 1, -- order column ascending or descending
	@paging_Count int out -- number of items in the collection
)
AS

CREATE TABLE #tmpTable 
(
	[TerritoryID] NVarChar(20)
)

DECLARE @total__ivqatedr int
DECLARE @orderByColumnIndex int
-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@paging_RecordsperPage * @paging_PageIndex)
IF (@total__ivqatedr <> 0) AND (@paging_PageIndex <> -1)
BEGIN
	SET ROWCOUNT @total__ivqatedr
END
INSERT INTO #tmpTable
(
	[territoryid]
)
SELECT
	[Territories].[territoryid]
FROM
[Territories]
WHERE
((@TerritoryID is null) OR ([Territories].[TerritoryID] LIKE @TerritoryID)) AND
((@TerritoryDescription is null) OR ([Territories].[TerritoryDescription] LIKE @TerritoryDescription)) AND
((@RegionID is null) OR ([Territories].[RegionID] = @RegionID))
ORDER BY
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'TerritoryID' THEN [Territories].[TerritoryID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'TerritoryID' THEN [Territories].[TerritoryID] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'TerritoryDescription' THEN [Territories].[TerritoryDescription] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'TerritoryDescription' THEN [Territories].[TerritoryDescription] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'RegionID' THEN [Territories].[RegionID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'RegionID' THEN [Territories].[RegionID] END END

	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [Territories].[created_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [Territories].[created_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [Territories].[created_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [Territories].[created_date] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [Territories].[modified_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [Territories].[modified_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [Territories].[modified_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [Territories].[modified_date] END END
-- set @paging_Count based on the rows moved in the previous statement
SET ROWCOUNT 0
SET @paging_Count = (
SELECT count(*)
FROM
[Territories]
WHERE
((@TerritoryID is null) OR ([Territories].[TerritoryID] LIKE @TerritoryID)) AND
((@TerritoryDescription is null) OR ([Territories].[TerritoryDescription] LIKE @TerritoryDescription)) AND
((@RegionID is null) OR ([Territories].[RegionID] = @RegionID))
)

-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@paging_RecordsperPage * @paging_PageIndex) - @paging_RecordsperPage
IF (@total__ivqatedr <> 0) AND (@paging_PageIndex <> -1)
BEGIN
	SET ROWCOUNT @total__ivqatedr
	DELETE FROM #tmpTable
END

-- return the number of rows requested as the page size
IF (@paging_PageIndex <> -1)
BEGIN
SET ROWCOUNT @paging_RecordsperPage
END
SELECT
	[Territories].[territoryid],
	[Territories].[territorydescription],
	[Territories].[regionid],
	[Territories].[created_by],
	[Territories].[created_date],
	[Territories].[modified_by],
	[Territories].[modified_date],
	[Territories].[time_stamp]
FROM
	[#tmpTable]
	INNER JOIN [Territories] ON #tmpTable.[territoryid] = [TERRITORIES].[territoryid]
ORDER BY
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'TerritoryID' THEN [Territories].[TerritoryID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'TerritoryID' THEN [Territories].[TerritoryID] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'TerritoryDescription' THEN [Territories].[TerritoryDescription] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'TerritoryDescription' THEN [Territories].[TerritoryDescription] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'RegionID' THEN [Territories].[RegionID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'RegionID' THEN [Territories].[RegionID] END END

	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [Territories].[created_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [Territories].[created_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [Territories].[created_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [Territories].[created_date] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [Territories].[modified_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [Territories].[modified_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [Territories].[modified_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [Territories].[modified_date] END END
DROP TABLE #tmpTable

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_TerritoryUpdate]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_TerritoryUpdate]
GO


SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_TerritoryUpdate]
(
	@TerritoryDescription NChar(50),
	@RegionID Int,
	@modified_by varchar (50),
	@modified_date datetime = null,
	@Original_TerritoryID NVarChar(20),
	@Original_time_stamp timestamp

)
AS

IF (@modified_date IS NULL)
SET @modified_date = GetDate();

SET NOCOUNT OFF;
UPDATE 
[Territories] 
SET
[RegionID] = @RegionID,
[TerritoryDescription] = @TerritoryDescription,
[modified_by] = @modified_by,
[modified_date] = @modified_date

WHERE
	[Territories].[TerritoryID] = @Original_TerritoryID AND
	[Territories].[time_stamp] = @Original_time_stamp


SELECT
	[Territories].[territoryid],
	[Territories].[territorydescription],
	[Territories].[regionid],
	[Territories].[created_by],
	[Territories].[created_date],
	[Territories].[modified_by],
	[Territories].[modified_date],
	[Territories].[time_stamp]
FROM 
[Territories]
WHERE
	[Territories].[TerritoryID] = @Original_TerritoryID
GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_TerritorySelectByCreatedDateRange]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_TerritorySelectByCreatedDateRange]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_TerritorySelectByCreatedDateRange]
(
	@start_date datetime,
	@end_date datetime
)
AS

SET NOCOUNT ON;

SELECT
		[Territories].[territoryid],
	[Territories].[territorydescription],
	[Territories].[regionid],
	[Territories].[created_by],
	[Territories].[created_date],
	[Territories].[modified_by],
	[Territories].[modified_date],
	[Territories].[time_stamp]

FROM
[Territories]
WHERE
((([Territories].[created_date] IS NULL) AND (@start_date IS NULL)) OR (@start_date <= [Territories].[created_date])) AND 
((([Territories].[created_date] IS NULL) AND (@end_date IS NULL)) OR (@end_date >= [Territories].[created_date]))
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_TerritorySelectByModifiedDateRange]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_TerritorySelectByModifiedDateRange]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_TerritorySelectByModifiedDateRange]
(
	@start_date datetime,
	@end_date datetime
)
AS

SET NOCOUNT ON;

SELECT
		[Territories].[territoryid],
	[Territories].[territorydescription],
	[Territories].[regionid],
	[Territories].[created_by],
	[Territories].[created_date],
	[Territories].[modified_by],
	[Territories].[modified_date],
	[Territories].[time_stamp]

FROM
[Territories]
WHERE
((([Territories].[modified_date] IS NULL) AND (@start_date IS NULL)) OR (@start_date <= [Territories].[modified_date])) AND 
((([Territories].[modified_date] IS NULL) AND (@end_date IS NULL)) OR (@end_date >= [Territories].[modified_date]))
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_TerritorySelectBySearchOr]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_TerritorySelectBySearchOr]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_TerritorySelectBySearchOr]
(
	@TerritoryID NVarChar(20) = null, 
	@TerritoryDescription NChar(50) = null, 
	@RegionID Int = null, 
	@paging_PageIndex int = -1, -- page number selected by the user
	@paging_RecordsperPage int = -1, -- number of items on the page
	@paging_OrderByColumn varchar(100) = '', -- name of column to order things by
	@paging_Ascending bit = 1, -- order column ascending or descending
	@paging_Count int out -- number of items in the collection
)
AS

CREATE TABLE #tmpTable 
(
	[TerritoryID] NVarChar(20)
)

DECLARE @total__ivqatedr int
DECLARE @orderByColumnIndex int
-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@paging_RecordsperPage * @paging_PageIndex)
IF (@total__ivqatedr <> 0) AND (@paging_PageIndex <> -1)
BEGIN
	SET ROWCOUNT @total__ivqatedr
END
INSERT INTO #tmpTable
(
	[territoryid]
)
SELECT
	[Territories].[territoryid]
FROM
[Territories]
WHERE
((@TerritoryID is null) OR ([Territories].[TerritoryID] LIKE @TerritoryID)) OR
((@TerritoryDescription is null) OR ([Territories].[TerritoryDescription] LIKE @TerritoryDescription)) OR
((@RegionID is null) OR ([Territories].[RegionID] = @RegionID))
ORDER BY
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'TerritoryID' THEN [Territories].[TerritoryID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'TerritoryID' THEN [Territories].[TerritoryID] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'TerritoryDescription' THEN [Territories].[TerritoryDescription] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'TerritoryDescription' THEN [Territories].[TerritoryDescription] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'RegionID' THEN [Territories].[RegionID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'RegionID' THEN [Territories].[RegionID] END END

	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [Territories].[created_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [Territories].[created_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [Territories].[created_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [Territories].[created_date] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [Territories].[modified_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [Territories].[modified_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [Territories].[modified_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [Territories].[modified_date] END END
-- set @paging_Count based on the rows moved in the previous statement
SET ROWCOUNT 0
SET @paging_Count = (
SELECT count(*)
FROM
[Territories]
WHERE
((@TerritoryID is null) OR ([Territories].[TerritoryID] LIKE @TerritoryID)) OR
((@TerritoryDescription is null) OR ([Territories].[TerritoryDescription] LIKE @TerritoryDescription)) OR
((@RegionID is null) OR ([Territories].[RegionID] = @RegionID))
)

-- remove top x values from the temp table based upon the specific page requested
SET @total__ivqatedr = (@paging_RecordsperPage * @paging_PageIndex) - @paging_RecordsperPage
IF (@total__ivqatedr <> 0) AND (@paging_PageIndex <> -1)
BEGIN
	SET ROWCOUNT @total__ivqatedr
	DELETE FROM #tmpTable
END

-- return the number of rows requested as the page size
IF (@paging_PageIndex <> -1)
BEGIN
SET ROWCOUNT @paging_RecordsperPage
END
SELECT
	[Territories].[territoryid],
	[Territories].[territorydescription],
	[Territories].[regionid],
	[Territories].[created_by],
	[Territories].[created_date],
	[Territories].[modified_by],
	[Territories].[modified_date],
	[Territories].[time_stamp]
FROM
	[#tmpTable]
	INNER JOIN [Territories] ON #tmpTable.[territoryid] = [TERRITORIES].[territoryid]
ORDER BY
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'TerritoryID' THEN [Territories].[TerritoryID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'TerritoryID' THEN [Territories].[TerritoryID] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'TerritoryDescription' THEN [Territories].[TerritoryDescription] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'TerritoryDescription' THEN [Territories].[TerritoryDescription] END END, 
	CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'RegionID' THEN [Territories].[RegionID] END END DESC, 
	CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'RegionID' THEN [Territories].[RegionID] END END

	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [Territories].[created_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_by' THEN [Territories].[created_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [Territories].[created_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'created_date' THEN [Territories].[created_date] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [Territories].[modified_by] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_by' THEN [Territories].[modified_by] END END
	,CASE @paging_Ascending WHEN 0 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [Territories].[modified_date] END END DESC
	,CASE @paging_Ascending WHEN 1 THEN CASE @paging_OrderByColumn WHEN 'modified_date' THEN [Territories].[modified_date] END END
DROP TABLE #tmpTable

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_TerritorySelect]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_TerritorySelect]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO

CREATE PROCEDURE [dbo].[gen_TerritorySelect]
AS

SET NOCOUNT ON;

SELECT
		[Territories].[territoryid],
	[Territories].[territorydescription],
	[Territories].[regionid],
	[Territories].[created_by],
	[Territories].[created_date],
	[Territories].[modified_by],
	[Territories].[modified_date],
	[Territories].[time_stamp]

FROM 
[Territories]
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[gen_TerritorySelectByRegionPks]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
	drop procedure [dbo].[gen_TerritorySelectByRegionPks]
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS ON 
GO



CREATE PROCEDURE [dbo].[gen_TerritorySelectByRegionPks]
(
	@xml ntext
)
AS

DECLARE @hDoc int
EXEC sp_xml_preparedocument @hDoc OUTPUT, @xml

SELECT
		[Territories].[territoryid],
	[Territories].[territorydescription],
	[Territories].[regionid],
	[Territories].[created_by],
	[Territories].[created_date],
	[Territories].[modified_by],
	[Territories].[modified_date],
	[Territories].[time_stamp]

FROM 
[Territories]
WHERE
	[Territories].[RegionID] IN (SELECT [RegionID]
				FROM OpenXML(@hDoc, '//Region', 2)
				WITH ([RegionID] Int 'RegionID'))

exec sp_xml_removeDocument @hDoc	

GO
SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

